package org.jeecg.modules.demo.weixin.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeFastpayRefundQueryModel;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import org.jeecg.modules.demo.weixin.dto.PayDto;
import org.jeecg.modules.demo.weixin.entity.PayOrder;
import org.jeecg.modules.demo.weixin.entity.SmtMerchantShopRecord;

import java.util.stream.Collectors;

public class ZhiFuBaoUtils {


    public static AlipayTradePayResponse zhifub(String orderNo, String appAuthToken, String SellerId, PayDto payDto, PayOrder payOrder) throws AlipayApiException {
        String privateKey = ZfbConfig.PRIVATE_KEY;
        String alipayPublicKey = ZfbConfig.ALIPAY_PUBLIC_KEY;
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(ZfbConfig.APP_ID);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePayRequest request = new AlipayTradePayRequest();
        request.putOtherTextParam("app_auth_token", appAuthToken);
        AlipayTradePayModel model = new AlipayTradePayModel();
        model.setOutTradeNo(orderNo);
        model.setTotalAmount(String.valueOf(payDto.getPayPrice()));
        if (payDto.getOrderType() == 6) {
            String collect = payDto.getShopRecordList().stream().map(SmtMerchantShopRecord::getShopName).collect(Collectors.joining(","));
            model.setSubject(collect);
        } else  model.setSubject(payOrder.getTrkName());
        model.setAuthCode(payDto.getAuthCode());
        model.setScene("bar_code");
        model.setSellerId(SellerId);
        request.setBizModel(model);
        AlipayTradePayResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println(response);
            return response;
        }
        System.out.println("receiptAmount" + response.getReceiptAmount());
        System.out.println("tradeNo" + response.getTradeNo());
        System.out.println("totalAmount" + response.getTotalAmount());
        return null;
    }


    public static AlipayTradeQueryResponse zfbPayQuery(String orderNo,String appAuthToken) throws AlipayApiException {
        String privateKey = ZfbConfig.PRIVATE_KEY;
        String alipayPublicKey = ZfbConfig.ALIPAY_PUBLIC_KEY;
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(ZfbConfig.APP_ID);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.putOtherTextParam("app_auth_token", appAuthToken);
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
//        model.setOutTradeNo(orderNo);
        model.setTradeNo(orderNo);
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("tradeStatus---------" + response.getTradeStatus());//TRADE_SUCCESS
            return response;
        }
        return null;
    }


    public static AlipayTradeRefundResponse zhifubTk(String outRequestNo, String orderNo, String appAuthToken,String refundAmount) throws AlipayApiException {
        String privateKey = ZfbConfig.PRIVATE_KEY;
        String alipayPublicKey = ZfbConfig.ALIPAY_PUBLIC_KEY;
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(ZfbConfig.APP_ID);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.putOtherTextParam("app_auth_token", appAuthToken);
        AlipayTradeRefundModel model = new AlipayTradeRefundModel();
        model.setRefundAmount(refundAmount);
        model.setOutTradeNo(orderNo);
        model.setOutRequestNo(outRequestNo);
        request.setBizModel(model);
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            return response;
        }
        return null;
    }


    public static AlipayTradeFastpayRefundQueryResponse tkCx(String OutRequestNo, String OutTradeNo, String appAuthToken) throws AlipayApiException {
        String privateKey = ZfbConfig.PRIVATE_KEY;
        String alipayPublicKey = ZfbConfig.ALIPAY_PUBLIC_KEY;
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(ZfbConfig.APP_ID);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
        request.putOtherTextParam("app_auth_token", appAuthToken);
        AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
        model.setOutRequestNo(OutRequestNo);
        model.setOutTradeNo(OutTradeNo);
        request.setBizModel(model);
        AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("RefundStatus" + response.getRefundStatus());
            return response;
        }
        return null;
    }
}
